package com.justcan.demo.cartoon.utils.anim;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;


/**
 * 用于动画绘图的View
 *
 * @author zhanghuijun
 */
public class AnimView extends View implements AnimFrameController.IAnimFrameListener, IAnimView {

    /**
     * 是否已经测量完成
     */
    protected boolean mHadSize = false;
    /**
     * 宽高
     */
    protected int mWidth = 0;
    protected int mHeight = 0;
    /**
     * 一组AnimObjectGroup
     */
    protected List<AnimObjectGroup> mAnimObjectGroups = null;
    /**
     * 动画帧控制器
     */
    protected AnimFrameController mAnimFrameController = null;
    /**
     * 动画时钟
     */
    protected AnimClock mAnimClock = null;

    public AnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AnimView(Context context) {
        super(context);
        init();
    }

    /**
     * 初始化
     */
    protected void init() {
        // 获取主线程的Looper，即发送给该Handler的都在主线程执行
        mAnimFrameController = new AnimFrameController(this, Looper.getMainLooper());
        mAnimObjectGroups = new ArrayList<AnimObjectGroup>();
        mAnimClock = new AnimClock();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHadSize = true;
        mWidth = w;        // 其实就等于getMeasuredWidth()和getMeasuredHeight()
        mHeight = h;
        for (int i = 0; i < mAnimObjectGroups.size(); i++) {
            mAnimObjectGroups.get(i).onSizeChange(w, h);
        }
        start();
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility == View.VISIBLE) {
            if (mHadSize) {
                start();
            }
        } else {
            stop();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stop();
    }

    /**
     * 开始
     */
    @Override
    public void start() {
        mAnimFrameController.start();
        mAnimClock.start();
    }

    /**
     * 停止
     */
    @Override
    public void stop() {
        mAnimFrameController.stop();
    }

    /**
     * 添加一个AnimObjectGroup
     */
    @Override
    public void addAnimObjectGroup(AnimObjectGroup group) {
        mAnimObjectGroups.add(group);
    }

    /**
     * 移除一个AnimObjectGroup
     */
    @Override
    public void removeAnimObjectGroup(AnimObjectGroup group) {
        mAnimObjectGroups.remove(group);
    }

    @Override
    public int getAnimSceneWidth() {
        return mWidth;
    }

    @Override
    public int getAnimSceneHeight() {
        return mHeight;
    }

    /**
     * 设置帧频
     */
    @Override
    public void setFtp(int ftp) {
        mAnimFrameController.setFtp(ftp);
    }

    /**
     * 绘制
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 逻辑
        for (int i = 0; i < mAnimObjectGroups.size(); i++) {
            mAnimObjectGroups.get(i).logic(mAnimClock.getAnimTime(), mAnimClock.getDeltaTime());
        }
        // 绘制
        for (int i = 0; i < mAnimObjectGroups.size(); i++) {
            mAnimObjectGroups.get(i).draw(canvas, mWidth, mHeight);
        }
        mAnimFrameController.updateFrame();
        mAnimClock.updateFrame();
    }

    @Override
    public void onUpdateFrame() {
        invalidate();
    }

}
